package com.ruoyi.gis.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gis.domain.GisDictData;
import com.ruoyi.gis.service.IGisDictDataService;
import com.ruoyi.gis.service.IGisDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典数据Controller
 *
 * @author rww
 * @date 2023-02-20
 */
@RestController
@RequestMapping("/gis/dict/data")
public class GisDictDataController extends BaseController
{
    @Autowired
    private IGisDictDataService gisDictDataService;

    @Autowired
    private IGisDictTypeService gisDictTypeService;

    /**
     * 查询字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('GisDictData:GisDictData:list')")
    @GetMapping("/list")
    public TableDataInfo list(GisDictData GisDictData)
    {
        startPage();
        List<GisDictData> list = gisDictDataService.selectGisDictDataList(GisDictData);
        return getDataTable(list);
    }

    /**
     * 导出字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('GisDictData:GisDictData:export')")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GisDictData GisDictData)
    {
        List<GisDictData> list = gisDictDataService.selectGisDictDataList(GisDictData);
        ExcelUtil<GisDictData> util = new ExcelUtil<GisDictData>(GisDictData.class);
        util.exportExcel(response, list, "字典数据数据");
    }

    /**
     * 获取字典数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('GisDictData:GisDictData:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable("dictCode") Long dictCode)
    {
        return success(gisDictDataService.selectGisDictDataByDictCode(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<GisDictData> data = gisDictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<GisDictData>();
        }
        return success(data);
    }

    /**
     * 新增字典数据
     */
    @PreAuthorize("@ss.hasPermi('GisDictData:GisDictData:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GisDictData GisDictData)
    {
        GisDictData.setCreateBy(getUsername());
        return toAjax(gisDictDataService.insertGisDictData(GisDictData));
    }

    /**
     * 修改字典数据
     */
    @PreAuthorize("@ss.hasPermi('GisDictData:GisDictData:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GisDictData GisDictData)
    {
        GisDictData.setUpdateBy(getUsername());
        return toAjax(gisDictDataService.updateGisDictData(GisDictData));
    }

    /**
     * 删除字典数据
     */
    @PreAuthorize("@ss.hasPermi('GisDictData:GisDictData:remove')")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        return toAjax(gisDictDataService.deleteGisDictDataByDictCodes(dictCodes));
    }
}
